import * as cesium from 'cesium/Cesium'
import * as turf from '@turf/turf'
import { vm, SEND } from '../../../utils/eventManage'
const Cesium = cesium

class RadarManage{
    constructor(mapControl){
        this.mapControl=mapControl;
        this.viewer=mapControl.viewer;
        this.ellipsoidList = []
        this.init();
    }
    init(){
        vm.$on(SEND.updateTime, this.updateTime)
    }
    handleDrawEllipsoid(position,radius) {
        console.log(position,radius,88888)
        this.clearEllipsoid()
        let ellipsoid = this.viewer.entities.add({
            position: position,
            ellipsoid: {
                radii: new Cesium.Cartesian3(radius, radius, radius),
                maximumCone: Cesium.Math.PI_OVER_TWO,
                //   material: Cesium.Color.BLUE.withAlpha(0.3),
                material: new Cesium.PolylineTrailWallMaterialProperty(Cesium.Color.BLUE.withAlpha(.9), 1000),
                //   outline: true,
            },
        });
        // let cartographic = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(position)
        let cartographic = this.mapControl.convert.cartesian3ToDegrees(position)
        // var radius = 0.2;
        var options = { steps: 30, units: 'kilometers', properties: { foo: 'bar' } };
        var circle = turf.circle([cartographic.longitude, cartographic.latitude], radius/1000, options);
        ellipsoid.geomPolygon = circle;
        this.ellipsoidList.push(ellipsoid);
    }
    clearEllipsoid() {
        for (let item of this.ellipsoidList) {
            this.viewer.entities.remove(item);
        }
        this.ellipsoidList = []
    }
    updateTime = (time) => {
        let modelEntityList = this.mapControl.modelList;
        let list = [];
        this.ellipsoidList.forEach(item => {
            modelEntityList.forEach(mItem => {
                // let cartographic = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(mItem.position)
                let cartographic = this.mapControl.convert.cartesian3ToDegrees(mItem.position)
                let pt = turf.point([cartographic.longitude, cartographic.latitude]);
                let isHasPoint = turf.booleanPointInPolygon(pt, item.geomPolygon);
                if(isHasPoint){
                    list.push(mItem);
                }else{

                }
            })
        })
        vm.$emit(SEND.updateRadarList, {list:list})
    }
}

export default RadarManage;